{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "39af9ecd",
   "metadata": {},
   "source": [
    "# Microsoft Word\n",
    "\n",
    ">[Microsoft Word](https://www.microsoft.com/en-us/microsoft-365/word) is a word processor developed by Microsoft.\n",
    "\n",
    "This covers how to load `Word` documents into a document format that we can use downstream."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9438686b",
   "metadata": {},
   "source": [
    "## Using Docx2txt\n",
    "\n",
    "Load .docx using `Docx2txt` into a document."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b80ea891",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install --upgrade --quiet  docx2txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7b80ea89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(page_content='Lorem ipsum dolor sit amet.', metadata={'source': './example_data/fake.docx'})]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_community.document_loaders import Docx2txtLoader\n",
    "\n",
    "loader = Docx2txtLoader(\"./example_data/fake.docx\")\n",
    "\n",
    "data = loader.load()\n",
    "\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d40727d",
   "metadata": {},
   "source": [
    "## Using Unstructured\n",
    "\n",
    "Please see [this guide](/docs/integrations/providers/unstructured/) for more instructions on setting up Unstructured locally, including setting up required system dependencies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "721c48aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(page_content='Lorem ipsum dolor sit amet.', metadata={'source': 'example_data/fake.docx'})]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_community.document_loaders import UnstructuredWordDocumentLoader\n",
    "\n",
    "loader = UnstructuredWordDocumentLoader(\"example_data/fake.docx\")\n",
    "\n",
    "data = loader.load()\n",
    "\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "525d6b67",
   "metadata": {},
   "source": [
    "### Retain Elements\n",
    "\n",
    "Under the hood, Unstructured creates different \"elements\" for different chunks of text. By default we combine those together, but you can easily keep that separation by specifying `mode=\"elements\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "064f9162",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Document(page_content='Lorem ipsum dolor sit amet.', metadata={'source': './example_data/fake.docx', 'category_depth': 0, 'file_directory': './example_data', 'filename': 'fake.docx', 'last_modified': '2023-12-19T13:42:18', 'languages': ['por', 'cat'], 'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'category': 'Title'})"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loader = UnstructuredWordDocumentLoader(\"./example_data/fake.docx\", mode=\"elements\")\n",
    "\n",
    "data = loader.load()\n",
    "\n",
    "data[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1f3b83f",
   "metadata": {},
   "source": [
    "## Using Azure AI Document Intelligence\n",
    "\n",
    ">[Azure AI Document Intelligence](https://aka.ms/doc-intelligence) (formerly known as `Azure Form Recognizer`) is machine-learning \n",
    ">based service that extracts texts (including handwriting), tables, document structures (e.g., titles, section headings, etc.) and key-value-pairs from\n",
    ">digital or scanned PDFs, images, Office and HTML files.\n",
    ">\n",
    ">Document Intelligence supports `PDF`, `JPEG/JPG`, `PNG`, `BMP`, `TIFF`, `HEIF`, `DOCX`, `XLSX`, `PPTX` and `HTML`.\n",
    "\n",
    "This current implementation of a loader using `Document Intelligence` can incorporate content page-wise and turn it into LangChain documents. The default output format is markdown, which can be easily chained with `MarkdownHeaderTextSplitter` for semantic document chunking. You can also use `mode=\"single\"` or `mode=\"page\"` to return pure texts in a single page or document split by page.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5bd47c2",
   "metadata": {},
   "source": [
    "## Prerequisite\n",
    "\n",
    "An Azure AI Document Intelligence resource in one of the 3 preview regions: **East US**, **West US2**, **West Europe** - follow [this document](https://learn.microsoft.com/azure/ai-services/document-intelligence/create-document-intelligence-resource?view=doc-intel-4.0.0) to create one if you don't have. You will be passing `<endpoint>` and `<key>` as parameters to the loader."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71cbdfe0",
   "metadata": {},
   "source": [
    "%pip install --upgrade --quiet  langchain langchain-community azure-ai-documentintelligence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "691bd9e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader\n",
    "\n",
    "file_path = \"<filepath>\"\n",
    "endpoint = \"<endpoint>\"\n",
    "key = \"<key>\"\n",
    "loader = AzureAIDocumentIntelligenceLoader(\n",
    "    api_endpoint=endpoint, api_key=key, file_path=file_path, api_model=\"prebuilt-layout\"\n",
    ")\n",
    "\n",
    "documents = loader.load()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
